package com.ten.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ten.common.core.entity.EduCourseCollect;
import com.ten.common.core.vo.Result;
import com.ten.course.dao.CourseListDao;
import com.ten.course.dao.EduCourseCollectDao;
import com.ten.course.service.EduCourseCollectService;
import com.ten.course.vo.CourseShouCangVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：张成（3092211427@qq.com）
 * @date ：Created in 2022/12/26 15:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class EduCourseCollectServiceImpl implements EduCourseCollectService {
    @Autowired
    private EduCourseCollectDao eduCourseCollectDao;


    /**
     * 根据课程id和memberId查询member有没有收藏该课程
     *
     * @param eduCourseCollect 包含课程id和memberId
     * @return
     */
    @Override
    public Result findFavoriteCourse(EduCourseCollect eduCourseCollect) {
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", eduCourseCollect.getCourseId());
        wrapper.eq("member_id", eduCourseCollect.getMemberId());
        EduCourseCollect eduCourseCollect1 = eduCourseCollectDao.selectOne(wrapper);
        return new Result(200, "查询成功", eduCourseCollect1);
    }

    /**
     * 根据课程id和会员id添加收藏
     * @param eduCourseCollect 包含课程id和memberId
     * @return
     */
    @Override
    public Result addFavoriteCourse(EduCourseCollect eduCourseCollect) {
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", eduCourseCollect.getCourseId());
        wrapper.eq("member_id", eduCourseCollect.getMemberId());
        EduCourseCollect eduCourseCollect1 = eduCourseCollectDao.selectOne(wrapper);
        if(eduCourseCollect1!=null){
            eduCourseCollect1.setIsDeleted(0);
            int i = eduCourseCollectDao.updateById(eduCourseCollect1);
            if(i>0) {
                return new Result(200, "收藏成功");
            }
        }
        int insert = eduCourseCollectDao.insert(eduCourseCollect);
        if(insert >0){
            return new Result(200,"收藏成功");
        }
        return new Result(500,"收藏失败");
    }

    @Override
    public Result deleteFavoriteCourse(EduCourseCollect eduCourseCollect) {
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", eduCourseCollect.getCourseId());
        wrapper.eq("member_id", eduCourseCollect.getMemberId());
        EduCourseCollect eduCourseCollect1 = eduCourseCollectDao.selectOne(wrapper);
        if(eduCourseCollect1!=null){
            eduCourseCollect1.setIsDeleted(1);
            int i = eduCourseCollectDao.updateById(eduCourseCollect1);
            if(i>0){
                return new Result(200,"取消收藏成功");
            }
        }
        return new Result(500,"取消收藏失败");
    }

    /**
     * 通过会员id查询所有收藏课程的信息
     * @param huiYuanId 会员id
     * @return
     */
    @Override
    public Result<List<CourseShouCangVo>> findFavoriteCourseById(String huiYuanId) {
        List<CourseShouCangVo> list = eduCourseCollectDao.findFavoriteCourseById(huiYuanId);
        return new Result<>(200,"查询成功",list);
    }

}